package com.zhw.client;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author weizhang52
 * @description:
 * @date 2019/6/11
 * @modify
 */
@Slf4j
public class ReConnectHandler implements ChannelFutureListener {


    @Override
    public void operationComplete(ChannelFuture channelFuture) throws Exception {

        log.info("[operationComplete] 进入");
        if (!channelFuture.isSuccess()) {
            final EventLoop loop = channelFuture.channel().eventLoop();
            loop.schedule(() -> {
                System.err.println("服务端链接不上，开始重连操作...");
//                    imConnection.connect(ImClientApp.HOST, ImClientApp.PORT);
                    HelloNettyClient.start();
//                channelFuture.channel().pipeline().fireChannelInactive();
                log.info("[operationComplete] 已执行重连");
            }, 1L, TimeUnit.SECONDS);
        } else {
            System.err.println("服务端链接成功...");
        }

    }

}
